﻿app.controller('DicSystemAddCtrl', function ($scope, $modalInstance, $interval, type, selected, dicSet, dicSystem) {
    var originalData;
    var lastData;
    $scope.message = '';
    dicSet.GetAll().success(function (data, header, config, status) {
        $scope.dicSets = data;
        var isEdit = (type == 'edit');
        if (isEdit) {
            $scope.type = '编辑';
            originalData = (selected && selected.length > 0) ? DeepClone(selected[0]) : null;
            $scope.dicSystem = selected[0];
            for (var i = 0; i < data.length; i++) {
                if (data[i].Key == selected[0].DimensionInfo.Key) {
                    $scope.dicSystem.DimensionInfo = data[i];
                    break;
                }
            }
        } else {
            $scope.type = '添加';
            $scope.dicSystem = {};
            $scope.dicSystem.StatusCode = 1;
        }
    }).error(function (data, header, config, status) { });

    $scope.specialFlagChange = function () {
        if ($scope.dicSystem.SpecialFlag == 1) {

        } else {
            $scope.dicSystem.SpecialTableName = '';
        }
    };

    $scope.getInputCode = function () {
        $scope.dicSystem.InputCode = GetFirstPinYinCode($scope.dicSystem.Value);
    };

    $scope.issueDateChange = function () {
        if ($scope.dicSystem.IssueDate > $scope.dicSystem.ImpDate) {
            $scope.message = '发布日期应在实施日期前！';
            $scope.dicSystem.IssueDate = null;
        } else {
            $scope.message = '';
        }
    };

    $scope.impDateChange = function () {
        if ($scope.dicSystem.IssueDate > $scope.dicSystem.ImpDate) {
            $scope.message = '实施日期应在发布日期后！';
            $scope.dicSystem.ImpDate = null;
        } else {
            $scope.message = '';
        }
    };

    $scope.ok = function () {
        $modalInstance.close($scope.dicSystem);
    };

    $scope.cancel = function () {
        if (originalData) {
            selected[0] = originalData;
        }
        if (lastData) {
            $modalInstance.dismiss(lastData);
            return;
        }
        $modalInstance.dismiss();
    };

    $scope.okNext = function () {
        dicSystem.Add($scope.dicSystem).success(function (data, header, config, status) {
            $scope.message = '添加成功！（3）';
            $scope.seconds = 2;
            var message = $interval(function () {
                if ($scope.seconds <= 0) {
                    $interval.cancel(message);
                    $scope.message = '';
                } else {
                    $scope.message = '添加成功！（' + $scope.seconds + '）';
                    $scope.seconds--;
                }
            }, 1000);
            //第一个输入框获取焦点
            $scope.isFocus = true;
            $scope.dicSystem.SerialNo = data;
            lastData = $scope.dicSystem;
            $scope.dicSystem = {};
            $scope.dicSystem.StatusCode = 1;
        }).error(function (data, header, config, status) {
            angular.alert('操作失败！');
        });
    };
});
